package com.laiketui.common.service.dubbo.openUser;

import com.laiketui.common.api.openUser.OpenUserService;
import com.laiketui.common.mapper.OpenUserMapper;
import com.laiketui.domain.openUser.OpenUser;
import com.laiketui.domain.user.User;
import com.laiketui.domain.vo.MainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OpenUserServiceImpl implements OpenUserService {

    @Autowired
    private OpenUserMapper openUserMapper;

    @Override
    public void updateOpenUser(User userInfo, String openid,int storeId) {
        String userId = userInfo.getUser_id();
        OpenUser openUser = new OpenUser();
        openUser.setUserId(userId);
        openUser.setStoreId(storeId);
        openUser = openUserMapper.selectOne(openUser);
        if(openUser == null){
            OpenUser insertOpenUser = new OpenUser();
            insertOpenUser.setUserId(userId);
            insertOpenUser.setStoreId(storeId);
            insertOpenUser.setOpenid(openid);
            openUserMapper.insertSelective(insertOpenUser);
        }else{
            OpenUser updateOpenUser = new OpenUser();
            updateOpenUser.setOpenid(openid);
            updateOpenUser.setId(openUser.getId());
            openUserMapper.updateByPrimaryKeySelective(updateOpenUser);
        }
    }

    @Override
    public String selectOpenidByUser(User userInfo, int storeId) {
        String openid = "";
        String userId = userInfo.getUser_id();
        OpenUser openUser = new OpenUser();
        openUser.setUserId(userId);
        openUser.setStoreId(storeId);
        openUser = openUserMapper.selectOne(openUser);
        if(openUser != null){
            openid = openUser.getOpenid();
        }
        return openid;
    }
}
